layui.define(['jquery', 'table', 'layer', 'form', 'laydate', 'common'], function (exports) {
    var $ = layui.jquery,
        layer = layui.layer,
        form = layui.form,
        common = layui.common,
        laydate = layui.laydate,
        table = layui.table;
    var table_text = null;
    //页面事件列表
    var active = {
        reload: function () {
            //执行重载
            table.reload('table_text', {
                page: {
                    curr: 1
                }
                , where: {

                }
            });
        },
        remove: function (ids) {
            common.ajaxRemove(prefix + "/remove", ids, function (res) {
                active['reload'].call(this);
            });
        }
    };
    var _config = null;
    var view = {
        init: function (config) {
            _config = config;
            laydate.render({
                elem: '.ss-laydate' //指定元素
            });
            this.initTable();
            this.initForm();
        },
        initTable: function () {
            table_text = table.render({
                elem: '#table_text',
                method: 'POST',
                url: prefix + "/listData", //数据接口
                toolbar: '#table_text_toolbar',
                done: function () {
                    $('#table_fans').next().css('height', 'auto');
                },
                limit: 20,
                height: 680,
                page: true,
                cols: new Array([
                    {
                        type: 'checkbox'
                    },
                    {
                        type: 'numbers',
                        title: '序号',
                        width:50
                    },
                    {
                        field: 'inputcode',
                        title: '关键词',
                        width: 150,
                        align: 'center'
                    },
                    {
                        field: 'title',
                        title: '标题',
                        width: 150,
                        align: 'center'
                    },
                    {
                        field: 'content',
                        title: '消息描述',
                        align: 'center'
                    },
                    {
                        field: 'lock',
                        title: '操作',
                        width: 150,
                        align: 'center',
                        toolbar: '#col_operation'
                    }
                    ])
            });


            //表格工具栏按钮事件
            table.on('toolbar(table_text)', function (obj) {
                switch (obj.event) {
                    case 'toolbar-text-add':

                        break;
                    case 'toolbar-text-remove':

                        break;

                }
            });
            //表格事件
            table.on('tool(table_text)', function (obj) {
                switch (obj.event) {
                    case 'edit'://同步粉丝
                        showDialog({
                            title: '修改',
                            template: 'edit',
                            saveUrl: '/msgtext/updateText',
                            tableObj: tableObj,
                            htmlData: data,
                        })
                        break;
                    case 'mass': //发送消息
                        showDialog({
                            title: '群发',
                            template: '/views/common/template/fans-chose.html',
                            height: 600,
                            yes:function (index) {
                                if(GLOBAL.choosed.length<2){
                                    layer.msg("至少选择2个粉丝");
                                    return false;
                                }
                                $.ajax({
                                    url: '/wxapi/massSendTextByOpenIds',
                                    data: {
                                        textId:data.id,
                                        openIds: GLOBAL.choosed.join(",")
                                    },
                                    success: function (result) {
                                        if ( result == "1") {
                                            layer.msg("群发成功");
                                            reloadTable(tableObj);
                                            layer.close(index);
                                        } else {
                                            layer.msg("群发失败");
                                        }
                                    }
                                })
                            },
                            end:function () {
                                GLOBAL.choosed=[];
                            }
                        });
                        break;

                }
            });


        },
        initForm: function () {
            $('form .layui-btn').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
                return false;
            });
        },
        removeItems: function () {
            active['remove'].call(this);
        },
        refreshTable: function () {
            active['reload'].call(this);
        }
    };
    exports('text', view);
});